package com.group3.customer.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group3.customer.cache.Car;
import com.group3.customer.service.ICustomerDishService;
import com.group3.customer.service.impl.CustomerDishServiceImpl;
import com.group3.customer.util.SenderUtil;
import com.group3.entity.Customer;
import com.group3.entity.Dish;

/**
 * Servlet implementation class CarServlet
 */
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ICustomerDishService dishService = new CustomerDishServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		car(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		car(request,response);
	}
	
	private void car(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String mId = request.getParameter("mId");
		String dId = request.getParameter("dId");
		String action = request.getParameter("action");
		Integer num = Integer.parseInt(request.getParameter("num"));
		System.out.println(mId + "..." + dId + "..." + action + "..." + num);
		boolean flag = true;
		HttpSession session = request.getSession();
		List<Car> cars = (List<Car>) session.getAttribute(mId);

		if(cars==null) {
			cars = new ArrayList<Car>();
			Dish dish = dishService.query(dId);
			Car car = new Car(dId, dish, num);
			cars.add(car);
			session.setAttribute(mId, cars);
		} else {
			Iterator<Car> itor = cars.iterator();
			while(itor.hasNext()) {
				Car c = itor.next();
				System.out.println("HasDiD: " + (c.getdId().equals(dId)));
				if(c.getdId().equals(dId)) {
					flag = false;
					switch(action) {
					case "reduce" :
						c.setNum(c.getNum() - num);
						break;
					case "add":
						c.setNum(c.getNum() + num);
						break;
					case "update":
						c.setNum(num);
						break;
					case "delete":
						System.out.println("before: " + cars.size());
						itor.remove();
						System.out.println("end: " + cars.size());
						break;
					}
				}
			}
//			for(Car c: cars) {
//				
//				System.out.println("HasDiD: " + (c.getdId().equals(dId)));
//				if(c.getdId().equals(dId)) {
//					flag = false;
//					switch(action) {
//					case "reduce" :
//						c.setNum(c.getNum() - num);
//						break;
//					case "add":
//						c.setNum(c.getNum() + num);
//						break;
//					case "update":
//						c.setNum(num);
//						break;
//					case "delete":
//						cars.remove(c);
//						break;
//					}
//				}
//			}
			System.out.println("Flag: " + flag);
			if(flag&&"add".equals(action)) {
				Dish dish = dishService.query(dId);
				Car car = new Car(dId, dish, num);
				cars.add(car);
			}
		}
		System.out.println(cars);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cars", cars);
		SenderUtil.send(map, response);
		
	}
}
